﻿@namespace ThingsGateway.Gateway.Razor
@using ThingsGateway.Admin.Application
@using ThingsGateway.Admin.Razor
@using ThingsGateway.Gateway.Application
@attribute [Authorize]
@attribute [RolePermission]
@inherits ComponentDefault


<div style="height: var(--table-height);padding: 1px;margin:auto;">

    <AdminTable @ref=table TItem="Device"
                AutoGenerateColumns="true"
                ShowAdvancedSearch=true
                EditDialogSize="Size.ExtraExtraLarge"
                AllowResizing="true"
                IsFixedHeader=true
                IsMultipleSelect=true
                CustomerSearchModel=SearchModel
                ShowExtendButtons=true
                ShowExportButton
                ScrollingDialogContent
                SearchMode=SearchMode.Top
                ShowDefaultButtons=true
                ExtendButtonColumnWidth=250
                OnQueryAsync="OnQueryAsync"
                IsPagination=true
                OnSaveAsync="Save"
                OnDeleteAsync="Delete">

        <TableColumns>
            <TableColumn @bind-Field="@context.PluginName" Align="Alignment.Center" Filterable=true Sortable=true>
                <Template Context="pluginName">
                    @PluginServiceUtil.GetFileNameAndTypeName(pluginName?.Value).TypeName
                </Template>
            </TableColumn>

            <TableColumn @bind-Field="@context.ChannelId" Align="Alignment.Center" Sortable=true>
                <Template Context="channelId">
                    @{
                        ChannelDict.TryGetValue(channelId.Value, out var channel);
                    }
                    @channel?.Name
                </Template>
            </TableColumn>

            <TableColumn @bind-Field="@context.RedundantDeviceId" Align="Alignment.Center" Sortable=true>
                <Template Context="deviceId">
                    @{
                        string device = null;
                        if (deviceId.Value.HasValue)
                            DeviceDict.TryGetValue(deviceId.Value.Value, out device);
                    }
                    @device
                </Template>

            </TableColumn>

        </TableColumns>
        <EditTemplate Context="context">
            <DeviceEditComponent Model=context PluginType=PluginType PluginNames="PluginNames" PluginDcit="PluginDcit" DeviceDict=DeviceDict RedundantDevicesQuery="OnRedundantDevicesQuery"></DeviceEditComponent>
        </EditTemplate>


        <ExportButtonDropdownTemplate Context="ExportContext">
            <Button class="dropdown-item" OnClick="() => ExcelExportAsync(ExportContext)" IsDisabled=@(!AuthorizeButton("导出"))>
                <i class="fas fa-file-export"></i>
                <span>@RazorLocalizer["TablesExportButtonExcelText"]</span>
            </Button>
            <Button class="dropdown-item" OnClick="() => ExcelImportAsync(ExportContext)" IsDisabled=@(!AuthorizeButton("导入"))>
                <i class="fas fa-file-import"></i>
                <span>@RazorLocalizer["TablesImportButtonExcelText"]</span>
            </Button>
        </ExportButtonDropdownTemplate>
        <TableToolbarTemplate>
            <TableToolbarButton TItem="Device" IsShow=@(AuthorizeButton(AdminOperConst.Edit))
                                Color=Color.Info Text="@RazorLocalizer["BatchEdit"]"
                                OnClickCallback=@(BatchEdit) />

            <TableToolbarPopConfirmButton TItem="Device"
                                          Color=Color.Warning Text="@Localizer["Clear"]" IsShow=@(AuthorizeButton("清空"))
                                          IsAsync OnConfirm=@(ClearDeviceAsync) />



        </TableToolbarTemplate>
        <RowButtonTemplate>
            <TableCellButton Size="Size.ExtraSmall" Color="Color.Success" Icon="fa-solid fa-people-roof" Text="@Localizer["RelationVariable"]" OnClick="()=>RelationVariableAsync(context.Id)" />
        </RowButtonTemplate>
        <CustomerSearchTemplate>
            @if (context is DeviceSearchInput model)
            {
                @Render(model)
            }
        </CustomerSearchTemplate>


    </AdminTable>
</div>

@code {
    AdminTable<Device> table;
}
@code {
    RenderFragment Render(DeviceSearchInput model) =>
    @<div class="row form-inline g-2">
        <div class="col-12 col-md-6">
            <Select IsVirtualize @bind-Value="model.ChannelId" Items="@ChannelNames" ShowSearch="true" ShowLabel="true" IsClearable DisableItemChangedWhenFirstRender=true  />
        </div>
        <div class="col-12 col-md-6">
            <BootstrapInput @bind-Value="model.Name" ShowLabel="true" />
        </div>
        <div class="col-12 col-md-6">
            <BootstrapInput @bind-Value="model.PluginName" ShowLabel="true" />
        </div>
    </div>;

}
